# NIC Makefile
# rocky 9.1 ignore the fallthrough warnings
rocky9_1=5.14.0-162.6.1.el9_1.x86_64
# rocky 9.2
rocky9_2=5.14.0-284.11.1.el9_2.x86_64
# Kylin
kylin_4_19_90_89=4.19.90-89.11.v2401.ky10.x86_64
kylin_4_19_90_52=4.19.90-52.22.v2207.ky10.aarch64
kylin_4_19_90_52_15=4.19.90-52.15.v2207.ky10.x86_64
kylin_4_19_90_52_22_x86=4.19.90-52.22.v2207.ky10.x86_64
# rhel9.0_x86
rhel9_0_x86=5.14.0-70.22.1.el9_0.x86_64
# rhel8.4_x86
rhel8_4_x86=4.18.0-305.el8.x86_64
# uos_1070e
uos_1070e=4.19.90-2403.3.0.0270.84.uel20.x86_64
# CentOS8.5
centos8_5=4.18.0-348.el8.aarch64
#UnionTech OS Server 20 1050u2e
uos_20_1050u2e=4.19.90-2211.5.0.0178.22.uel20.aarch64

ifeq ($(rocky9_1), $(shell uname -r))
EXTRA_CFLAGS += -DROCKY9_VERSION
else ifeq ($(rocky9_2), $(shell uname -r))
EXTRA_CFLAGS += -DROCKY9_VERSION
EXTRA_CFLAGS += -DROCKY92
else ifeq ($(kylin_4_19_90_89), $(shell uname -r))
EXTRA_CFLAGS += -DKYLIN_4_19
else ifeq ($(kylin_4_19_90_52), $(shell uname -r))
EXTRA_CFLAGS += -DKYLIN_4_19
else ifeq ($(kylin_4_19_90_52_15), $(shell uname -r))
EXTRA_CFLAGS += -DKYLIN_4_19
else ifeq ($(kylin_4_19_90_52_22_x86), $(shell uname -r))
EXTRA_CFLAGS += -DKYLIN_4_19
else ifeq ($(shell uname -r), $(rhel9_0_x86))
EXTRA_CFLAGS += -DROCKY9_VERSION
EXTRA_CFLAGS += -DROCKY92
else ifeq ($(rhel8_4_x86), $(shell uname -r))
EXTRA_CFLAGS += -DREDHAT8_6
else ifeq ($(uos_1070e), $(shell uname -r))
EXTRA_CFLAGS += -DKYLIN_4_19
EXTRA_CFLAGS += -DUOS_1070E
else ifeq ($(centos8_5), $(shell uname -r))
EXTRA_CFLAGS += -DREDHAT8_6
else ifeq ($(uos_20_1050u2e), $(shell uname -r))
EXTRA_CFLAGS += -DUOS_4_19_90
else ifneq (,$(findstring ky10,$(shell uname -r)))
# Fallback for KY10 4.19 kernels not exactly listed above
ifeq (,$(findstring 5.,$(shell uname -r)))
EXTRA_CFLAGS += -DKYLIN_4_19
ccflags-y    += -DKYLIN_4_19
endif
endif

EXPORT_SYMBOL := true

TOP_DIR := $(PWD)/../../

EXTRA_CFLAGS += -I$(TOP_DIR)/platform/huawei_secure_c/include
#UDK
EXTRA_CFLAGS += -I$(TOP_DIR)/include/mpu
EXTRA_CFLAGS += -I$(TOP_DIR)/include/public
EXTRA_CFLAGS += -I$(TOP_DIR)/include/cfg_mgmt
EXTRA_CFLAGS += -I$(TOP_DIR)/include/cqm
EXTRA_CFLAGS += -I$(TOP_DIR)/include/bond
EXTRA_CFLAGS += -I$(TOP_DIR)/host/include
EXTRA_CFLAGS += -I$(TOP_DIR)/host/mt
EXTRA_CFLAGS += -I$(TOP_DIR)/host/ossl/include
EXTRA_CFLAGS += -I$(TOP_DIR)/host/ossl/linux/kernel

EXTRA_CFLAGS += -DHIUDK_SDK -DHIUDK_ULD -DHIUDK_OPENEULER -DHAVE_KS_SUPPORT
EXTRA_CFLAGS += -Werror -Wno-implicit-fallthrough

# Stack protector flags: some KYLIN KY10 4.19 aarch64 kernels don't export
# __stack_chk_guard/__stack_chk_fail to modules. Disable for those kernels.
ifneq ($(findstring ky10,$(shell uname -r)),)
ccflags-y += -fno-stack-protector
else
EXTRA_CFLAGS += -fstack-protector-all
endif

obj-m += hiudk3.o 
hiudk3-objs := ../../platform/huawei_secure_c/src/securecutil.o \
            ../../platform/huawei_secure_c/src/secureinput_a.o \
            ../../platform/huawei_secure_c/src/secureprintoutput_a.o \
            ../../platform/huawei_secure_c/src/memset_s.o \
            ../../platform/huawei_secure_c/src/memcpy_s.o \
            ../../platform/huawei_secure_c/src/strncpy_s.o \
            ../../host/vram/vram_common.o \
            hiudk_com_intf.o \
            hiudk_cqm_com_intf.o \
            hiudk_cqm_obj_intf.o \
            hiudk_crm_com_intf.o \
            hiudk_crm_svr_intf.o \
            hiudk_dev_intf.o \
            hiudk_hw_com_intf.o \
            hiudk_hw_reg_intf.o \
            hiudk_lld.o \
            hiudk_wq_intf.o

SYS_TIME=$(shell date +%Y-%m-%d_%H:%M:%S)
ccflags-y += -D __TIME_STR__=\"$(SYS_TIME)\"
GLOBAL_VERSION=$(shell cat $(TOP_DIR)/GLOBAL_VERSION_NEW | grep driver | awk -F ':' '{print $$2}')
ccflags-y += -DGLOBAL_VERSION_STR=\"$(GLOBAL_VERSION)\"

$(warning cflags, $(ccflags-y))
V ?= 0

ccflags-y += -DHW_CONVERT_ENDIAN

ccflags-y += -D__LINUX__

EXTRA_CFLAGS += -DSECUREC_EXPORT_KERNEL_SYMBOL=0

KERNEL_VER := $(shell uname -r 2>null)

KERNEL_DIR := /lib/modules/$(KERNEL_VER)/build

default:
	$(MAKE) -C $(KERNEL_DIR) M=$(shell pwd) -W modules

clean:
	rm -rf *.o *.ko *.order .*.cmd *.mod.* .H* .tm* .tmp_versions Module.symvers *.ko.unsigned null
